package jp.oesf.rssreader;

import jp.oesf.rssreader.model.RowModel; 
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SummaryList extends ListActivity {

	private TableAdapter adapter = null;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary);

        //Intentからパラメータ"category"の内容を取得する
        String category = null;
        try {
		    Bundle extras = getIntent().getExtras();
			category = extras.getString("category");
		}catch (Exception e) {}

		//categoryの内容をViewに設定する
		TextView textCategory = (TextView)findViewById(R.id.summary_category);
		if (category != null) {
			textCategory.setText(category);
		}
        //Tableadapterを作成し、activityに設定する
		adapter = new TableAdapter(this);
        setListAdapter(adapter);
        //TableAdapterにRowModelを設定する
        setRowModel(adapter,category);
	}

	private void setRowModel(TableAdapter adapter, String category) {
        RssSQLiteOpenHelper databaseOpenHelper = new RssSQLiteOpenHelper(this);
        SQLiteDatabase database = databaseOpenHelper.getReadableDatabase();
        //全件検索する（_idの昇順）
        Cursor cursor;
        if (category == null) {
            //全件検索する（_idの昇順）
            cursor = database.query(RssSQLiteOpenHelper.RSS_TABLE,null,null,null,null,null,"_id");
        } else {
            //指定されたcategoryに該当するものを検索する（_idの昇順）
            cursor = database.query(RssSQLiteOpenHelper.RSS_TABLE,null,"category=?",new String[]{category},null,null,null);
        	
        }
        //テーブル１件毎にRowModelを作成し、TableAdapterに追加する
        while (cursor.moveToNext()) {
        	RowModel row = new RowModel();
            row.setId(cursor.getLong(cursor.getColumnIndex("_id")));
            row.setTitle(cursor.getString(cursor.getColumnIndex("title")));
        	adapter.add(row);
        }
        databaseOpenHelper.close();
	}
		
	public class TableAdapter extends ArrayAdapter<RowModel> {

		public TableAdapter(Context context) {
			super(context,R.layout.summary);
		}
 
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			RowModel item = getItem(position);
			if (row == null) {
				//Viewを作成する
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.summary_row, null);
			}
			if ( item!= null) {
				// Title
				TextView textTitle = (TextView)row.findViewById(R.id.summary_title);
				if (textTitle != null) {
					textTitle.setText(item.getTitle());
				}
			}
			return row;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
 		
		Intent intent = new Intent(this, Detail.class);
		RowModel row = adapter.getItem(position);
		intent.putExtra("id", row.getId());
		startActivity(intent);
	}
}